package Offer.offer2017;

/**
 * 旋转数组的最小值
 * Created by 雷文康 on 2017/12/29.
 */
public class Test08 {
    public static void main(String[] args) {
        int[]array={3,4,5,6,7,1,2};
        System.out.println(findMin(array));
    }
    public static int findMin(int[] array){
        if (array == null || array.length == 0) {
            return -1;
        }
        if (array[0] < array[array.length - 1]){
            return array[0];
        }
        if (array[0] == array[array.length - 1] && array[0] == array[(array.length -1)/2]){
            return orderFindMin(array);
        }
        int start = 0;
        int end = array.length - 1;
        int mid;
        while (end - start != 1) {
            mid = (start + end) / 2;
            if (array[mid] >= array[start]) {
                start = mid + 1;
            } else if (array[mid] <= array[end]){
                end = mid;
            }
        }
        return array[end];
    }

    private static int orderFindMin(int[] array){
        int min = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] < min){
                min = array[i];
            }
        }
        return min;
    }
}
